export default {
	data() {
		return {
			nums: 0
		}
	},
	onLoad() {
		this.countTabNum()
	},
	methods: {
		jiarugwBtn(item) {
			console.log(item)
			const that = this
			if (item.stock == 0) {
				uni.showToast({
					icon: 'none',
					mask: true,
					title: '库存补充中'
				});
				return
			}
			that.$api.request('cart', 'addCartItem', {
				skuId: item.skuId,
				num: 1
			}).then(res => {
				that.$api.msg('添加购物车成功')
				this.$emit('change')
			})
		},
		//获取购物车数量
		countTabNum() {
			this.$api.request('cart', 'countCart').then(res => {
				this.nums = res.data
				console.log(this.nums)
				if (res.data > 0) {
					uni.setTabBarBadge({
						index: 2,
						text: res.data + ''
					})
				} else if (res.data <= 0) {
					uni.removeTabBarBadge({
						index: 2
					})
				}
				this.$store.commit('addCart', res.data)
			}).catch(err => {})
		},
	}
}
